import simplejson
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect, get_object_or_404
from my_example.models import *
# Create your views here.
from my_example import Attribute_Evaluation


def ex1(request):
    if request.method == "POST":
        form = EX1_Form(request.POST)
        if form.is_valid():
            form.save()
        return redirect("/")
    else:
        form = EX1_Form()
    return render(request, "EX1.html", locals())


def mianban(request, pk):
    ren = renwu.objects.get(pk=pk)
    jns = get_jineng(ren)
    zbs = get_zb(ren)

    # 计算属性结果
    minjie = Attribute_Evaluation.minjie_result(ren.pk)
    liliang = Attribute_Evaluation.liliang_result(ren.pk)
    zhili = Attribute_Evaluation.zhili_result(ren.pk)
    ganzhi = Attribute_Evaluation.ganzhi_result(ren.pk)

    wushang = Attribute_Evaluation.wushang_result(pk)
    gongci = Attribute_Evaluation.gongci_result(pk)

    pikan = Attribute_Evaluation.pikan_result(pk)
    chuanci = Attribute_Evaluation.chuanci_result(pk)
    mogong = Attribute_Evaluation.mogong_result(ren.pk)

    moming = Attribute_Evaluation.moming_result(ren.pk)
    mochuan = Attribute_Evaluation.mochuan_result(ren.pk)
    zhiming = Attribute_Evaluation.zhiming_result(ren.pk)

    data = {
        "力量": liliang,
        "敏捷": minjie,
        "智力": zhili,
        "感知": ganzhi,
        "武器伤害": wushang,
        '攻击次数': gongci,
        "劈砍": pikan,
        "穿刺": chuanci,
        "魔攻": mogong,
        "魔命": moming,
        '魔穿': mochuan,
        '致命': zhiming
    }
    return render(request, "人物面板.html", locals())


def get_jineng(ren):
    renwu_jinengs = renwu_jieng.objects.filter(renwu=ren).all()
    for jn in renwu_jinengs:
        jn_name = jn.jineng.name
        # print(jn_name)
        jn_attrs = jn.jineng.attrs.all()
        # print(jn_attrs)
    return renwu_jinengs


def get_zb(ren):
    renwu_zbs = renwu_zhuangbei.objects.filter(renwu=ren).all()

    return renwu_zbs

def post_zb(request):
    '''
    提交装备数据,json格式
    {
        "name":xx,
        "leixing":xx,
        "attrs_json":xx
    }
    :param request:
    :return:
    '''
    zb_dict={}

    try:
        req=simplejson.loads(request.raw_post_data)
    except Exception as e:
        print(e)




def result(request, pk):
    ren = get_object_or_404(renwu, pk=pk)
    result = Attribute_Evaluation.get_gongci_type(ren)
    print(result)
    return HttpResponse(1)


def pass_audit(request,pk):
    zb=get_object_or_404(Submit_ZhuangBei,pk=pk)

    try:
        # 添加装备
        new_zb=zhuangbei(name=zb.name,leixing=zb.leixing)
        new_zb.save()

        # 关联装备和属性
        data = json.loads(zb.attrs_json)
        for item in data['attr_data']:
            attr=get_object_or_404(jineng_attrs,pk=int(item['id']))
            lsobj=zhuangbei_key_value(zhuangbei=new_zb,attr_name=attr,attr_value=float(item['value']))
            lsobj.save()

        zb.is_save=True

        zb.save()

    except Exception as e:
        print(e)

    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))